package DecertifiSys.service;

import DecertifiSys.bean.TToken;
import DecertifiSys.dao.TTokenMapper;
import DecertifiSys.untill.baseUntill.AllTokenBring;
import DecertifiSys.untill.baseUntill.Map_objectUntill;
import DecertifiSys.untill.baseUntill.MessageUntill;
import DecertifiSys.untill.baseUntill.TimeUntil;
import com.sun.org.apache.regexp.internal.RE;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by zhoumeineng on 2018/1/31.
 */
@Service
public class TokenImpl {

    @Autowired
    TTokenMapper tTokenMapper;

    /**
     * 验证登录token
     */
    @Transactional
    public JSONObject autnToken(String uid,String token){
     TToken tToken = tTokenMapper.selectTokenInfo(uid);
        boolean equel = tToken.getLogintoken().equals(token);
     return MessageUntill.messageBody(null,equel?"0000":"0009",equel?"aa":"aaaa");
    }


    /**
     * 生成登录token
     * @param phoneNum 手机号
     * @param uid 用户id
     * @param  ip 网络的ip
     * 生成方式 手机号+时间 然后进行MD5加密
     *  Transactional 事务管理
     * */
    @Transactional
    public JSONObject bringToken(String phoneNum, String uid, String ip){
        /**
         * 生成 loginToken
         *
         * */
        String loginToken = AllTokenBring.loginTokenBring(phoneNum+ TimeUntil.getCurrentTime(1));
        TToken tToken = new TToken();
        tToken.setLogintoken(loginToken);
        tToken.setUid(uid);
        tToken.setLogintime(TimeUntil.getCurrentTime(2));
        tTokenMapper.insertLoginToken(tToken);
        JSONObject jsonObject=MessageUntill.messageBody(null,"0006","token生成失败");;
        try {
            jsonObject =  MessageUntill.messageBody(Map_objectUntill.objectToMap(tToken),"0000","登录成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject;
    }


    /**
     * 交易Token
     * */
   @Transactional
    public JSONObject bringToken(String logingToken){
       TToken tToken = new TToken();
       tToken.setTradetoken("S"+logingToken);
       tTokenMapper.upTokenInfo(tToken);
       return MessageUntill.messageBody(null,"0000","succed");
   }

    /**
     *
     * token登录注销
     * */
    @Transactional
    public JSONObject cancelToken(String uid){
        tTokenMapper.upLoginToken(uid);
       return MessageUntill.messageBody(null,"","");
    }
}
